﻿Imports System.Data.OleDb

Public Class EditUserProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (Not IsPostBack) Then
            BindGender()
            BindUserProfile()
        End If

    End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fuProfilePicture.FileName)
        If fuProfilePicture.HasFile Then
            fuProfilePicture.SaveAs(Server.MapPath("~/Account/ProfilePictures") & "/" & newFileName)
        End If
        Dim p1 As New OleDbParameter("@f1", txtFirstName.Text)
        Dim p2 As New OleDbParameter("@f2", txtLastName.Text)
        Dim p3 As New OleDbParameter("@f3", ddlGender.SelectedIndex)

        Dim p4 As New OleDbParameter("@f4", "~/Account/ProfilePictures" & "/" & newFileName)
        Dim p5 As New OleDbParameter("@f5", txtDOB.Text)
        Dim p6 As New OleDbParameter()
        p6.ParameterName = "@f6"
        If cbEmailSubscription.Checked = True Then
            p6.Value = "True"
        Else
            p6.Value = "False"
        End If


        Dim p7 As New OleDbParameter("@f7", User.Identity.Name)
        Dim sql As String
        Dim result As Integer
        If fuProfilePicture.HasFile Then
            sql = "Update UserProfile set FirstName=@f1,LastName=@f2,Gender=@f3,UserPicture=@f4,DateOfBirth=@f5,EmailSubscription=@f6 where UserName=@f7"
            result = DataAccessLayer.ExecuteNonQuery(sql, p1, p2, p3, p4, p5, p6, p7)
        Else
            sql = "Update UserProfile set FirstName=@f1,LastName=@f2,Gender=@f3,DateOfBirth=@f5,EmailSubscription=@f6 where UserName=@f7"
            result = DataAccessLayer.ExecuteNonQuery(sql, p1, p2, p3, p5, p6, p7)
        End If


        If result > 0 Then
            Response.Redirect("~/Account/UserProfile.aspx?ID=" & User.Identity.Name)
        End If


    End Sub
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Private Sub BindGender()
        Dim dsRole = DataAccessLayer.ExecuteDataSet("Select * from  Gender")
        ddlGender.DataSource = dsRole
        ddlGender.DataTextField = "Gender"
        ddlGender.DataValueField = "ID"
        ddlGender.DataBind()
        ddlGender.Items.Insert(0, New ListItem("Select", "0"))
    End Sub

    Private Sub BindUserProfile()

        Dim p1 As New OleDbParameter("@f1", User.Identity.Name)
        Using oledbReader As OleDbDataReader = DataAccessLayer.ExecuteDataReader("Select * from UserProfile where username= @f1", p1)
            While oledbReader.Read
                If Not IsDBNull(oledbReader.Item(1)) Then
                    txtFirstName.Text = oledbReader.Item(1)
                End If
                If Not IsDBNull(oledbReader.Item(2)) Then
                    txtLastName.Text = oledbReader.Item(2)
                End If

                If Not IsDBNull(oledbReader.Item(3)) Then
                    ddlGender.SelectedValue = oledbReader.Item(3)
                End If

                If Not IsDBNull(oledbReader.Item(4)) Then
                    userProfileImage.ImageUrl = oledbReader.Item(4)
                Else
                    userProfileImage.Visible = False
                End If

                If Not IsDBNull(oledbReader.Item(5)) Then
                    txtDOB.Text = oledbReader.Item(5)
                End If
                If Not IsDBNull(oledbReader.Item(10)) Then
                    If oledbReader.Item(10) = "True" Then
                        cbEmailSubscription.Checked = True
                    Else
                        cbEmailSubscription.Checked = False
                    End If
                End If
            End While
            oledbReader.Close()
        End Using
    End Sub

End Class